home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sound / nes_defs.h < prev    next >
C/C++ Source or Header  |  2000-04-23  |  4KB  |  206 lines

  1. /*****************************************************************************
  2.  
  3.   MAME/MESS NES APU CORE
  4.  
  5.   Based on the Nofrendo/Nosefart NES N2A03 sound emulation core written by
  6.   Matthew Conte (matt@conte.com) and redesigned for use in MAME/MESS by
  7.   Who Wants to Know? (wwtk@mail.com)
  8.  
  9.   This core is written with the advise and consent of Matthew Conte and is
  10.   released under the GNU Public License.  This core is freely avaiable for
  11.   use in any freeware project, subject to the following terms:
  12.  
  13.   Any modifications to this code must be duly noted in the source and
  14.   approved by Matthew Conte and myself prior to public submission.
  15.  
  16.  *****************************************************************************
  17.  
  18.    NES_DEFS.H
  19.  
  20.    NES APU internal type definitions and constants.
  21.  
  22.  *****************************************************************************/
  23.  
  24. #ifndef NESTYPES_H
  25. #define NESTYPES_H
  26.  
  27. /* BOOLEAN CONSTANTS */
  28. #ifndef TRUE
  29. #define TRUE   1
  30. #define FALSE  0
  31. #endif
  32.  
  33. /* REGULAR TYPE DEFINITIONS */
  34. typedef char          int8;
  35. typedef int           int16;
  36. typedef long          int32;
  37. typedef unsigned char uint8;
  38. typedef unsigned int  uint16;
  39. typedef unsigned long uint32;
  40. typedef char          boolean;
  41.  
  42.  
  43. /* QUEUE TYPES */
  44. #ifdef USE_QUEUE
  45.  
  46. #define QUEUE_SIZE 0x2000
  47. #define QUEUE_MAX  (QUEUE_SIZE-1)
  48.  
  49. typedef struct queue_s
  50. {
  51.   int pos;
  52.   unsigned char reg,val;
  53. } queue_t;
  54.  
  55. #endif
  56.  
  57. /* REGISTER DEFINITIONS */
  58. #define  APU_WRA0    0x00
  59. #define  APU_WRA1    0x01
  60. #define  APU_WRA2    0x02
  61. #define  APU_WRA3    0x03
  62. #define  APU_WRB0    0x04
  63. #define  APU_WRB1    0x05
  64. #define  APU_WRB2    0x06
  65. #define  APU_WRB3    0x07
  66. #define  APU_WRC0    0x08
  67. #define  APU_WRC2    0x0A
  68. #define  APU_WRC3    0x0B
  69. #define  APU_WRD0    0x0C
  70. #define  APU_WRD2    0x0E
  71. #define  APU_WRD3    0x0F
  72. #define  APU_WRE0    0x10
  73. #define  APU_WRE1    0x11
  74. #define  APU_WRE2    0x12
  75. #define  APU_WRE3    0x13
  76.  
  77. #define  APU_SMASK   0x15
  78.  
  79. #define  NOISE_LONG     0x4000
  80. #define  NOISE_SHORT    93
  81.  
  82. /* CHANNEL TYPE DEFINITIONS */
  83.  
  84. /* Square Wave */
  85. typedef struct square_s
  86. {
  87.    uint8 regs[4];
  88.    int vbl_length;
  89.    int freq;
  90.    float phaseacc;
  91.    float output_vol;
  92.    float env_phase;
  93.    float sweep_phase;
  94.    uint8 adder;
  95.    uint8 env_vol;
  96.    boolean enabled;
  97. } square_t;
  98.  
  99. /* Triangle Wave */
  100. typedef struct triangle_s
  101. {
  102.    uint8 regs[4]; /* regs[1] unused */
  103.    int linear_length;
  104.    int vbl_length;
  105.    int write_latency;
  106.    float phaseacc;
  107.    float output_vol;
  108.    uint8 adder;
  109.    boolean counter_started;
  110.    boolean enabled;
  111. } triangle_t;
  112.  
  113. /* Noise Wave */
  114. typedef struct noise_s
  115. {
  116.    uint8 regs[4]; /* regs[1] unused */
  117.    int cur_pos;
  118.    int vbl_length;
  119.    float phaseacc;
  120.    float output_vol;
  121.    float env_phase;
  122.    uint8 env_vol;
  123.    boolean enabled;
  124. } noise_t;
  125.  
  126. /* DPCM Wave */
  127. typedef struct dpcm_s
  128. {
  129.    uint8 regs[4];
  130.    uint32 address;
  131.    uint32 length;
  132.    int bits_left;
  133.    float phaseacc;
  134.    float output_vol;
  135.    uint8 cur_byte;
  136.    boolean enabled;
  137.    boolean irq_occurred;
  138.    uint8 *cpu_mem;
  139.    signed char vol;
  140. } dpcm_t;
  141.  
  142. /* APU type */
  143. typedef struct apu
  144. {
  145.    /* Sound channels */
  146.    square_t   squ[2];
  147.    triangle_t tri;
  148.    noise_t    noi;
  149.    dpcm_t     dpcm;
  150.  
  151.    /* APU registers */
  152.    unsigned char regs[22];
  153.  
  154.    /* Sound pointers */
  155.    void *buffer;
  156.  
  157. #ifdef USE_QUEUE
  158.  
  159.    /* Event queue */
  160.    queue_t queue[QUEUE_SIZE];
  161.    int head,tail;
  162.  
  163. #else
  164.  
  165.    int buf_pos;
  166.  
  167. #endif
  168.  
  169. } apu_t;
  170.  
  171. /* CONSTANTS */
  172.  
  173. /* vblank length table used for squares, triangle, noise */
  174. static const uint8 vbl_length[32] =
  175. {
  176.    5, 127, 10, 1, 19,  2, 40,  3, 80,  4, 30,  5, 7,  6, 13,  7,
  177.    6,   8, 12, 9, 24, 10, 48, 11, 96, 12, 36, 13, 8, 14, 16, 15
  178. };
  179.  
  180. /* frequency limit of square channels */
  181. static const int freq_limit[8] =
  182. {
  183.    0x3FF, 0x555, 0x666, 0x71C, 0x787, 0x7C1, 0x7E0, 0x7F0,
  184. };
  185.  
  186. /* table of noise frequencies */
  187. static const int noise_freq[16] =
  188. {
  189.    4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 2046
  190. };
  191.  
  192. /* dpcm transfer freqs */
  193. const int dpcm_clocks[16] =
  194. {
  195.    428, 380, 340, 320, 286, 254, 226, 214, 190, 160, 142, 128, 106, 85, 72, 54
  196. };
  197.  
  198. /* ratios of pos/neg pulse for square waves */
  199. /* 2/16 = 12.5%, 4/16 = 25%, 8/16 = 50%, 12/16 = 75% */
  200. static const int duty_lut[4] =
  201. {
  202.    2, 4, 8, 12
  203. };
  204.  
  205. #endif
  206.